/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet COMBO
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter :
tabData		 array of  value
 					 the array is :						
						-a list of array which contain a couple value/text
						(ex: [[val1,text1],...,[valn,textn]]) 		
 Name:               Code name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 width:              Width 
 parent              object parent

 objPar              object of parameter
	suggest			 suggest on key press
	enable			 init enable status
	heightItem		 height of an item in the list	
 	
*************************************************************************************************/


// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.Combo=function (tabData,name,top,left,width,parent,objPar)
{
	//base object parameter
    var objParam=new Object;
  	objParam.name=name;
	objParam.type="combo";
    objParam.left=left;
	objParam.top=top;
	objParam.width=width;
	
	if (objPar != null){
		objParam.position=objPar.position;
	}
	
	//base constructor
	this.base = rialto.widget.AbstractComponent;    
    this.base(objParam);

		
	//Default value
    this.enable=true;
    this.suggest=true;
    this.open=false;
    this.heightItem=23;
	//set the value with the properties of the objPar object
    if (objPar != null){
		if (rialto.lang.isBoolean(objPar.suggest)){
            this.suggest = objPar.suggest;        
        }
        if (rialto.lang.isBoolean(objPar.enable)){
            this.enable = objPar.enable;
        }             
        if (rialto.lang.isNumber(objPar.heightItem)){
            this.heightItem = objPar.heightItem;        
        }   
	}   
	
	//release object
	objPar=null;
	objParam=null;
	this.base=null;

    //Current index   
    this.currentIndex=-1;
    
         
    var oThis=this;       

    this.divExt.id=this.id+"_divExt";
    this.divExt.style.position=this.position;
    this.divExt.style.width=this.width;
    this.divExt.style.left=this.left+"px";    
    this.divExt.style.top=this.top+"px";    
    
    
    //hidden input to store the valus associted
    this.textValue=new rialto.widget.Text(this.name,0,0,'',"Hi",this.divExt);
    
    
    this.text=new rialto.widget.Text(this.name+"_text",0,0,parseInt(this.width)-14,"A",this.divExt,{autoUp:false,position:'absolute'});
    this.text.champs.className = "combo-input";
    this.text.champs.style.border="1px solid rgb(120,172,255)";
    
    if(this.suggest){
	    this.text.champs.onkeyup=function(e){                
	        if (!e) var e = window.event;
	        //check the keypress               
	        return oThis.checkKeyPress(e);	      
	        stopEvent(e);
	    }
	    //Bugg with ENTER if we leave the keypress of the field ??
   	    this.text.champs.onkeypress=function(e){
   	        if (!e) var e = window.event;
   	        return true;
   	    	stopEvent(e);
   	    };
	}
    
    this.text.champs.onblur = function() {    	
    	oThis.setOpen(false);
    }
    
    
    
    //Button to open or close the list
    this.bouton = document.createElement("DIV");   
    this.img=new rialto.widget.Image("bigDownArrow",0,0,this.bouton,"open","",{position:'absolute'});    
    this.bouton.className = "combo-bouton";
    
    this.img.onclick =function (){        
        if(oThis.enable){
		    //if the current list is different from the init list set data
	    	if(oThis.initListItem.length!=oThis.currentListItem.length){
		    	oThis.currentListItem=new Array;
		    	for(i=0;i<oThis.initListItem.length;i++){			        
		           oThis.currentListItem.push(oThis.initListItem[i]);
			    }
	    	}
		    //Open or Close		    
	        oThis.setOpen(!oThis.open);
	  	}
	}

    this.divExt.appendChild(this.bouton);
        
    //List
    var obj={name:"MENU-COMBO"
            ,posFixe:true
            ,width:parseInt(this.divExt.style.width)-4
            ,className:"combo-list"}
    this.opslist=new rialto.widget.simpleMenu(obj);              
    
    //onClose definition
    this.opslist.onClose=function(){
        oThis.setOpen(false);                    
    }
    //onClick definition
    this.opslist.onclick=function(item){
        //use the index propertie of the item        
        oThis.selInd(item.index);              
    }
    
    //onOver definition
    this.opslist.onOver=function (item){         
        //change the last over (it may have been choose by key press)
        if (oThis.lastOver && oThis.lastOver!=item){	        
            oThis.lastOver.out();
        }     
        //save the last over (if use the key press after)
        oThis.lastOver=item;
        oThis.currentIndex=item.index;
    }
    
    //add to the parent
    if (parent) {this.placeIn(parent);}; 
    
    //Initial enble status
    if(!this.enable){    	
	    this.setEnable(false);
	}
	
	//Data
    this.setData(tabData)
    
} 

rialto.widget.Combo.prototype = new rialto.widget.AbstractComponent;

rialto.widget.Combo.prototype.release=function (){
	this.text.champs.onkeypress=null;
	this.text.champs.onkeyup=null;
	this.text.champs.onblur=null;
	this.opslist.onClose=null;
	this.opslist.onclick=null;
	this.opslist.onOver=null;
	this.img.onclick =null;
	
	this.img.remove();
	this.text.remove();
	this.textValue.remove();
	this.opslist.remove();
}
//To set a new array of data
rialto.widget.Combo.prototype.setData=function (tabData)
{
    //Array of data
    this.tabData=tabData;
    this.initListItem=new Array();
    this.currentListItem=new Array();
    
    for(i=0;i<this.tabData.length;i++){
        var item=new ComboItem(this.tabData[i][0],this.tabData[i][1]);
        this.initListItem.push(item);  
        this.currentListItem.push(item);       
    }
}

rialto.widget.Combo.prototype.checkKeyPress=function (evt){
    //KEYCODE ACCORDING TO THE BROWSER
    var keyCode = evt.keyCode ? evt.keyCode :
                            evt.charCode ? evt.charCode :
                                            evt.which ? evt.which : void 0;
    
  
    if(keyCode==38 ||keyCode==40 || keyCode==13){	    	            	
	    if(!this.open){	
		    this.prepareList();    	
	    	this.setOpen(true);	
	    }	    
	    switch (keyCode)
	        {	        
	            case 38:    // up in the list              
	               if (this.currentIndex>0){
	               	this.currentIndex-=1;
	               }
	               else{
	               	this.currentIndex=0
	               }	               
	               //keep the last over and change his classname
	               if(this.lastOver){this.lastOver.out();}
	               this.lastOver=this.opslist.tabItem[this.currentIndex];
	               this.lastOver.over();
	               break;
	            
	            case 40:    //down in the list
	               if (this.currentIndex<this.currentListItem.length-1){
	               	this.currentIndex+=1;
	               };               
	               //keep the last over and change his classname
	               if(this.lastOver){this.lastOver.out();}
	               this.lastOver=this.opslist.tabItem[this.currentIndex];
	               this.lastOver.over();
	               break;
	            
	            case 13:    //select (enter)    		                
	                if (this.currentIndex>=0){                                       
	                    this.opslist.onclick(this.lastOver);	                    
	                }               
	                break;
	            
	            default:                        	                
	    		    break;
	        }
	        return false;
	    }
	    else{
		    /*if(keyCode==9 || (keyCode==37) || (keyCode==39) ||(keyCode==46) || keyCode==8){
	    		return true
	    	};*/
			//Check the text to display according to the key press
		    this.prepareList();
		    //Move in the list
		    if(this.currentListItem.length>0)
		    {			        
		        this.setOpen(true);			        
		    }
		    else{
		        //no array according so close the list
		        this.setOpen(false);
		    }	  
		    return true  
	    }
        
}
//******those 2 METHODS ARE DEPRECATED...USE SETOPEN
//to open the list
rialto.widget.Combo.prototype.open=function (){    
    rialto.deprecated('Combo','open','setOpen(true)');
    this.setOpen(true);
}

//to close the list
rialto.widget.Combo.prototype.close=function (){           
	rialto.deprecated('Combo','close','setOpen(false)');
    this.setOpen(false);
}
//******

rialto.widget.Combo.prototype.setOpen=function (open){    	        
    //if open fill the list with the current list item
    if(open){
		this.displayList(this.currentListItem);
    }    
    if(open==this.open){return;}
    this.open=open;
    if(open){
	    //change the image reference
	    this.img.setImageReference("bigUpArrow");        
	    //fix the top and left of the list
	    this.opslist.setPosTop(compOffsetTop(this.divExt)+22);
	    this.opslist.setPosLeft(compOffsetLeft(this.divExt)+2);
	    //open the list	    
	    this.opslist.affichezoneMenu(evt=null,heightCombo=22);
	}
	else{
		this.img.setImageReference("bigDownArrow");    
		this.currentIndex=-1;
		if(this.opslist.ouvert){this.opslist.fermezoneMenu();}
	}        
}


//this function will prepare an array with text that correspond to the text in the input field
rialto.widget.Combo.prototype.prepareList=function ()
{
    //initialise the tempory array
    this.currentListItem=new Array;
    //type string
    strSaisi = this.text.getValue().toLowerCase();         
    //For each item in the list
    for(i=0;i<this.initListItem.length;i++){
        //Ok if it start with the same string 
        if(this.initListItem[i].text.toLowerCase().indexOf(strSaisi)==0){
           this.currentListItem.push(this.initListItem[i]);
        }
    }     
}

//this function display the item of the array 
rialto.widget.Combo.prototype.displayList=function (arr)
{
    //clear the list
    this.opslist.clear();            	
    this.lastOver=null;
    this.currentIndex=-1;
    for(var i=0;i<arr.length;i++)
    {  
        var obj={text:""+arr[i].text,height:this.heightItem,bNotOut:true}        
        var item=this.opslist.addItem(obj)        
        //add the value associted
        item.val=arr[i].value;        
        item.index=i;        
    }    
}



rialto.widget.Combo.prototype.addItem=function(valeur,text)
{
    //add an item to the combo list
    var combItem=new ComboItem(valeur,text); 
    this.initListItem.push(combItem); 
    this.currentListItem.push(combItem);    
}


//******those 2 METHODS ARE DEPRECATED...USE SETVISIBLE
//to hide
rialto.widget.Combo.prototype.masque=function() {
	rialto.deprecated('Combo','masque','setVisible(false)');
    this.setVisible(false); 
}
//to display
rialto.widget.Combo.prototype.affiche=function() {
	rialto.deprecated('Combo','affiche','setVisible(true)');
    this.setVisible(true);
}
//******


//******SEL FUNCTIONS

rialto.widget.Combo.prototype.selLast=function (){    
    //to select the last item
    var indLast=this.initListItem.length-1;
    this.selInd(indLast);
}

//to select an item using his index in the list
rialto.widget.Combo.prototype.selInd=function (ind){    
	if(ind>=0 && ind<this.currentListItem.length){
		//close the list
	    if(this.opslist.ouvert){this.setOpen(false);}
	    //text and value
	    var text=this.currentListItem[ind].text;
	    var value=this.currentListItem[ind].value;
		//this.selItem(val,text);
		//fill the input text
	    this.text.setValue(text);    
	    //fill the input valut
	    this.textValue.setValue(value);    
	    //call the onclick method
	    this.onclick(value,text);
	 }
}

rialto.widget.Combo.prototype.selFirst=function (){    
     //to select the first item
    this.selInd(0);
}

//sel an item according to his value
rialto.widget.Combo.prototype.selWithValue=function (value){
    //find the index of this value
    var ind=this.indexOf(value);
    if (ind!=-1){
	    this.selInd(ind);
    }    
}

//sel an item according to his text
rialto.widget.Combo.prototype.selWithText=function (text){
   	//find the index of this text
    var ind=this.indexOfText(text);
    if (ind!=-1){
	    this.selInd(ind);
    }   
}
//find the first index of the item having this value (-1 if not)
rialto.widget.Combo.prototype.indexOf=function (value){
    
    for(i=0;i<this.initListItem.length;i++)
    {
        if (this.initListItem[i].value==value){            
            return i;
        }
    }
    //no item with this value
    return -1;
}

//find the first index of the item having this text (-1 if not)
rialto.widget.Combo.prototype.indexOfText=function (text){
    
    for(i=0;i<this.initListItem.length;i++)
    {
        if (this.initListItem[i].text==text){            
            return i;
        }
    }
    //no item with this text
    return -1;
}

//******those 2 METHODS ARE DEPRECATED...USE SETENABLE
//to enable
rialto.widget.Combo.prototype.active=function() {
	rialto.deprecated('Combo','active','setEnable(true)');
    this.setEnable(true);    
}
//to desable
rialto.widget.Combo.prototype.inactive=function() {
	rialto.deprecated('Combo','inactive','setEnable(false)');
    this.setEnable(false);
}
//******


//this method will allow to enable or disable the button according to the parameter
rialto.widget.Combo.prototype.setEnable=function(enable){	
	if(enable){		
	    this.text.setEnable(true);	     
	}
	else{		
	    this.text.setEnable(false);
	}
	this.text.champs.className = "combo-input";
    this.text.champs.style.border="1px solid rgb(120,172,255)";
	this.enable=enable;
}


//give the focus
rialto.widget.Combo.prototype.setFocus=function() {	
    this.text.setFocus();
}


//return the sel value of the combo
rialto.widget.Combo.prototype.getSelValue=function (){
   return this.textValue.getValue();
}

//return the sel value of the combo
rialto.widget.Combo.prototype.getValue=function (){
   return this.text.getValue();
}

//return the sel text of the combo
rialto.widget.Combo.prototype.getSelText=function (){
   return this.text.getValue();
}

//return the text associated of the item having this index
rialto.widget.Combo.prototype.getText=function (ind){          
	if(ind>=0 && ind<this.initListItem.length)
		return this.initListItem[ind].text
	else return null   
}

//return the value associated of the item having this index
rialto.widget.Combo.prototype.getValue=function (ind){          
	if(ind>=0 && ind<this.initListItem.length)
		return this.initListItem[ind].text    
	else return null   
}

//The code must be redefine after the instanciation of the component. By default this method does nothing.
//to do after select an item
rialto.widget.Combo.prototype.onclick=function (value,text){}

function ComboItem(value,text){   
         this.text  = text;
         this.value = value;
	}
